package com.animallife.android.adapters;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.animallife.android.R;
import com.animallife.android.db.AnimalQuery;
import com.animallife.android.imLoader.ImageLoaderNew;

public class AnimalAdapter extends CursorAdapter {

	private LayoutInflater mInflater;
	private ImageLoaderNew imLoader;

	public AnimalAdapter(Context context, Cursor c) {
		super(context, c, false);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imLoader = new ImageLoaderNew(context);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final WarrantyItemViewHolder listItem = (WarrantyItemViewHolder) view.getTag();
		final String warrantyText = cursor.getString(AnimalQuery.NAME);
		final String imageUrl = cursor.getString(AnimalQuery.URL_DROPBOX);
		final String musicKey = cursor.getString(AnimalQuery.SOUND);
		final int isAvalible = cursor.getInt(AnimalQuery.ISAVAILABLE);

		listItem.txtPurchaseAndStrone.setText(warrantyText);

		if (null != imageUrl && !TextUtils.isEmpty(imageUrl)) {
			listItem.imageItem.setTag(imageUrl);
			imLoader.displayImage(imageUrl, (Activity) context, listItem.imageItem);
		} else {
			listItem.imageItem.setImageResource(R.drawable.ic_launcher);
		}
		if (musicKey == null)
			listItem.item_sound.setVisibility(View.INVISIBLE);
		else
			listItem.item_sound.setVisibility(View.VISIBLE);

		switch (isAvalible) {
		case 0:
			listItem.item_lock.setVisibility(View.VISIBLE);
			break;
		case 1:
			listItem.item_lock.setVisibility(View.INVISIBLE);
			break;

		default:
			break;
		}

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.animals_list_item, null);
		final WarrantyItemViewHolder listItem = new WarrantyItemViewHolder();
		listItem.txtPurchaseAndStrone = (TextView) view.findViewById(R.id.item_title);
		listItem.imageItem = (ImageView) view.findViewById(R.id.item_image);
		listItem.item_lock = (ImageView) view.findViewById(R.id.item_lock);
		listItem.item_sound = (ImageView) view.findViewById(R.id.music_key);

		view.setTag(listItem);
		return view;

	}

	public class WarrantyItemViewHolder {
		TextView txtPurchaseAndStrone;
		ImageView imageItem;
		ImageView item_lock;
		ImageView item_sound;

	}

}
